/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.sms.dao.SmsStoreGroupLogDao;
import com.chlitina.store.modules.sms.entity.SmsStoreGroupLog;
import com.chlitina.store.modules.sms.utils.SmsUtils;

/**
 * 店面群发短信Service
 * @author lvbo
 * @version 2013-06-21
 */
@Component
@Transactional(readOnly = true)
public class SmsStoreGroupLogService extends BaseService {

	private static Logger logger = LoggerFactory.getLogger(SmsStoreGroupLogService.class);
	
	@Autowired
	private SmsStoreGroupLogDao smsStoreGroupLogDao;
	
	public SmsStoreGroupLog get(Long id) {
		return smsStoreGroupLogDao.findOne(id);
	}
	
	public Page<SmsStoreGroupLog> find(Page<SmsStoreGroupLog> page, SmsStoreGroupLog smsStoreGroupLog) {
		DetachedCriteria dc = smsStoreGroupLogDao.createDetachedCriteria();
		dc.addOrder(Order.desc("id"));
		return smsStoreGroupLogDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public List<SmsStoreGroupLog> findByParameter(Long storeId, String start, String end) {
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		return smsStoreGroupLogDao.findByParameter(storeId, startDate, endDate);
	}
	
	@Transactional(readOnly = false)
	public Long getRealCount(Long storeId, String start, String end) {
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		return smsStoreGroupLogDao.countByParameter(SmsUtils.default_length, storeId, startDate, endDate);
	}
	
	@Transactional(readOnly = false)
	public Long getSendCount(Long storeId, String start, String end) {
		DetachedCriteria dc = smsStoreGroupLogDao.createDetachedCriteria();
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		dc.add(Restrictions.eq("store.id", storeId));
		dc.add(Restrictions.between("planSendTime", startDate, endDate));
		return smsStoreGroupLogDao.count(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(SmsStoreGroupLog smsStoreGroupLog) {
		smsStoreGroupLogDao.save(smsStoreGroupLog);
	}
}
